package algotithm.leetcode.dfs.test200;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;

/**
 * @author zhouyanxiang
 * @Date 2021-01-2021/1/13-22:41
 */
public class Main {

    public static void main(String[] args) throws IOException {
        BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
        String[] num = reader.readLine().split(" ");
        int n = Integer.parseInt(num[0]);
        int m = Integer.parseInt(num[1]);
        int[][] grid = new int[n][m];
        int result = 0;
        int t = Integer.parseInt(num[0]);
        while (t-- > 0) {
            int i = n - t - 1;
            String[] str = reader.readLine().split(" ");
            for (int j = 0; j < m; j++) {
                grid[i][j] = Integer.parseInt(str[j]);
            }
        }
        reader.close();
        for (int i = 0; i < n; i++) {
            for (int j = 0; j < m; j++) {
                if (grid[i][j] == 1) {
                    result++;
                    dfs(grid,i,j,n,m);
                }
            }
        }
        System.out.println(result);
    }

    public static void dfs(int[][] grid,int i, int j,int n,int m) {
        if (i < 0 || j < 0 || i >= n || j >= m) {
            return;
        }
        if (grid[i][j] == 0) {
            return;
        }
        grid[i][j] = 0;
        dfs(grid,i-1,j,n,m);
        dfs(grid,i+1,j,n,m);
        dfs(grid,i,j+1,n,m);
        dfs(grid,i,j-1,n,m);
    }


}
